<template>
  <div>
    <a-card :bordered="false">
      <f-search-form :more="false" @searchHandler="searchHandler">
        <f-search-form-item label="流程名称" name="name"/>
      </f-search-form>
      <f-table ref="table" rowKey="processId" :columns="columns" :multiple="false" :operator="operator" :params="queryParam" :api="api.process">
        <span slot="action" slot-scope="text, record">
          <template>
            <a @click="$refs.FlowDesigner.show(record)">编辑</a>
            <a-divider type="vertical"/>
          </template>
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down"/></a>
            <a-menu slot="overlay">
               <a-menu-item>
                <a href="javascript:;" @click="processImg(record)">流程图</a>
              </a-menu-item>
               <a-menu-item>
                <a href="javascript:;" @click="$refs.drawer.handleShow(record)">流程授权</a>
              </a-menu-item>
                <a-menu-item>
                <a href="javascript:;" @click="processDeploy(record)">部署</a>
              </a-menu-item>
               <a-menu-item>
                <a href="javascript:;" @click="suspendOrActivate(record)">{{record.suspended?'激活':'挂起' }}</a>
              </a-menu-item>
              <a-menu-item v-if="$auth('table.delete')">
                <a href="javascript:;" @click="$refs.table.del(record,{'cascade':false})">删除</a>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </f-table>
      <process-drawer ref="drawer"></process-drawer>
      <flyme-flow-designer ref="FlowDesigner" style="overflow: hidden;"></flyme-flow-designer>
      <process-image ref="processImg" v-if="processImageVisible" :visible.sync="processImageVisible" :processDefinitionId="processDefinitionId"></process-image>
      <upload-modal ref="upload" tip="只能上传.bpmn20.xml,.bpmn,.bar,.zip文件，且不超过512K" :action="api.processDefinition.upload" accept=".bpmn20.xml,.bpmn,.bar,.zip" @change="uploadSuccess"></upload-modal>
    </a-card>
  </div>
</template>
<script>
  import processDrawer from './process_drawer.vue'
  import api from './js/api'
  import ProcessImage from './process_image'

  export default {
    name: 'TableList',
    components: {
      ProcessImage,
      processDrawer
    },
    data() {
      let me = this
      //操作按钮
      let operator = []
      operator.push({
        name: '添加',
        icon: 'plus',
        that: this,
        handler: function(table, that) {
          that.$refs.FlowDesigner.show()
        }
      })
      operator.push({
        name: '导入',
        icon: 'plus',
        that: this,
        handler: function(table, that) {
          that.$refs.upload.open()
        }
      })
      operator.push({
        name: '删除',
        icon: 'delete',
        batch: true,
        handler: function(table) {
          table.remove()
        }
      })
      return {
        api: Object.assign(api, this.api),
        //列表初始化查询参数
        queryParam: {},
        test: false,
        processImageVisible: false,
        processDefinitionId: '',
        // 表头
        columns: [
          {
            title: '#',
            width: '60',
            align: 'center',
            customRender: (text, record, index) => index + 1
          },
          {
            title: '流程key',
            align: 'center',
            dataIndex: 'processKey'
          },
          {
            title: '流程名称',
            align: 'center',
            dataIndex: 'processName'
          },
          {
            title: '版本',
            align: 'center',
            dataIndex: 'versionTag'
          },
          {
            title: '流程状态',
            align: 'center',
            dataIndex: 'suspended',
            customRender: (text, record, index) => {
              return text ? '挂起' : '激活'
            }
          },
          {
            title: '创建时间',
            align: 'center',
            dataIndex: 'createTime'
          },
          {
            title: '更新时间',
            align: 'center',
            dataIndex: 'updateTime'
          },
          {
            title: '操作',
            align: 'center',
            scopedSlots: { customRender: 'action' }
          }
        ],
        operator: operator
      }
    },
    created() {
    },
    methods: {
      processImg(process) {
        this.processDefinitionId = process.procDefId
        this.processImageVisible = true
      },
      uploadSuccess() {
        this.$refs['table'].refresh(false)
        this.$refs.upload.close()
        this.$message.success('导入成功')
      },
      processDeploy(process) {
        this.http.post(this, {
          url: this.api.process.deploy,
          data: {
            procModelId: process.procModelId,
            processId: process.processId
          },
          success(me, data) {
            me.$refs['table'].refresh(false)
          }
        })
      },
      suspendOrActivate(process) {
        let suspendOrActivate = process['suspended'] ? 'activate' : 'suspend'
        let url = this.api.processDefinition.setState + '/' + suspendOrActivate
        this.http.put(this, {
          url: url,
          data: {
            processDefinitionId: process.procDefId
          },
          success(me, data) {
            me.$refs['table'].refresh(false)
          }
        })
      }
    }
  }
</script>
